package com.example.demo.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.demo.domain.userInfo.UserInfo;
import com.example.demo.service.userInfo.UserInfoService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.domain.AjaxResult;

@Controller
public class LoginController extends BaseController {
	
    @Autowired
	private UserInfoService userInfoService;
	@GetMapping("/login")
	public String login(HttpServletRequest request, HttpServletResponse response) {
		return "login";
	}

	// 这里如果不写method参数的话，默认支持所有请求，如果想缩小请求范围，还是要添加method来支持get, post等等某个请求。
	@PostMapping("loginIn")
	@ResponseBody
	public AjaxResult login(String username, String password, Boolean rememberMe, Map<String, Object> map)
			throws Exception {
		System.out.println("Login Called：" + username + " " + password);
		Subject currentUser = SecurityUtils.getSubject();
		String msg = " ";
		// 把用户名和密码封装为 UsernamePasswordToken 对象
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);

		// remembermMe记住密码
		if (rememberMe != null)
			token.setRememberMe(rememberMe);
		try {
			// 执行登录.
			currentUser.login(token);
			// 登录成功...
			return success();
		} catch (IncorrectCredentialsException e) {
			msg = "登录密码错误";
			System.out.println("登录密码错误!!!" + e);
			return error("登录密码错误");
		} catch (ExcessiveAttemptsException e) {
			msg = "登录失败次数过多";
			System.out.println("登录失败次数过多!!!" + e);
			return error(msg);
		} catch (LockedAccountException e) {
			msg = "帐号已被锁定";
			System.out.println("帐号已被锁定!!!" + e);
			return error(msg);
		} catch (DisabledAccountException e) {
			msg = "帐号已被禁用";
			System.out.println("帐号已被禁用!!!" + e);
			return error(msg);
		} catch (ExpiredCredentialsException e) {
			msg = "帐号已过期";
			System.out.println("帐号已过期!!!" + e);
			return error(msg);
		} catch (UnknownAccountException e) {
			msg = "帐号不存在";
			System.out.println("帐号不存在!!!" + e);
			return error(msg);
		} catch (UnauthorizedException e) {
			msg = "您没有得到相应的授权！";
			System.out.println("您没有得到相应的授权！" + e);
			return error(msg);
		} catch (Exception e) {
			System.out.println("出错！！！" + e);
			return error(msg);
		}

	}
	@RequestMapping("logout")
	public String logout() {
		Subject currentUser = SecurityUtils.getSubject();
		try {
			currentUser.logout();
		} catch (AuthenticationException e) {
			e.printStackTrace();

		}
		return "login";
	}


	@RequestMapping("/403")
	public String unauthorizedRole() {
		System.out.println("------没有权限-------");
		return "403";
	}
}